#include <iostream>
#include <map>
#include <set>
using namespace std;

void find_ancestor(int inOder[],int inL,int inR,int preOder[],int preL,int preR,map<int,int>& ancestor,int root){
	if( preL <= preR ){
		int i = inL;
		for(; inOder[i]!=preOder[preL];i++);
		ancestor[inOder[i]] = root;
		int left_len = i - inL;
		find_ancestor(inOder,inL,i-1,preOder,preL+1,preL+left_len,ancestor,preOder[preL]);
		find_ancestor(inOder,i+1,inR,preOder,preL+left_len+1,preR,ancestor,preOder[preL]);
	}
}
int main(){
	int m,n;
	cin >> m >> n;
	int preOder[n],inOder[n];
	map<int,int> ancestor;
	for(int i=0; i<n; i++) cin >> inOder[i];
	for(int i=0; i<n; i++) cin >> preOder[i];
	int root = preOder[0];
	find_ancestor(inOder,0,n-1,preOder,0,n-1,ancestor,root);
	int u,v;	
    map<int,set<int>> anc_of_v;
	for(int i=0; i<m; i++){
		cin >> u >> v;
		if( ancestor.find(u)==ancestor.end() ){
			if( ancestor.find(v)==ancestor.end() ) cout << "ERROR: "<<u <<" and "<<v<<" are not found."<<endl;
			else cout << "ERROR: "<<u<<" is not found."<<endl;
		}else if( ancestor.find(v)==ancestor.end()) cout << "ERROR: "<<v<<" is not found."<<endl;
		else{
			int ut = u,vt = v,anc  = root;
			if( anc_of_v.find(v)==anc_of_v.end() )
			    for(; vt!=root; vt=ancestor[vt]) anc_of_v[v].insert(vt);
			while(ut!=root){
				if( anc_of_v[v].find(ut)!=anc_of_v[v].end() ) {
					anc = ut;
					break;
				}
				ut = ancestor[ut];
			}
			if( anc==u ) cout << u << " is an ancestor of " << v <<  "."<<endl;
			else if( anc==v ) cout << v << " is an ancestor of " << u <<"."<< endl;
			else cout << "LCA of "<< u << " and " << v << " is " << anc <<"."<< endl; 
		}
	} 
	return 0;
}
